package org.jsola.hr.helper;

import org.jsola.common.DateKit;
import org.jsola.common.SafeKit;
import org.jsola.hr.constant.ProgTypeEnum;
import org.jsola.hr.dto.*;
import org.jsola.hr.entity.SubsidyInfoDO;
import org.jsola.hr.entity.SubsidyProgDO;
import org.jsola.hr.query.SubsidyProgQuery;
import org.jsola.hr.service.ISubsidyProgService;
import org.jsola.hr.vo.EmpInfoVO;
import org.jsola.hr.vo.SubsidyProgListVO;
import org.jsola.hr.vo.SubsidyProgVO;
import org.jsola.module.ModuleContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

import static org.jsola.hr.core.AbstractTest.SITE_ID;
import static org.jsola.hr.core.TestCommonConstans.CODE_EMP_LIST;
import static org.jsola.hr.core.TestCommonConstans.SUBSIDY_PROG_VO_LIST;


/**
 * @author zhr
 */
@Component
public class SubsidyProgTestHelper extends BaseTestHelper {
    
    @Autowired
    private ISubsidyProgService subsidyProgService;

    /**
     * 生成add测试数据
     * @return SubsidyProgAddDTO
     */
    public SubsidyProgAddDTO getSubsidyProgAddDTO(){
        SubsidyProgAddDTO subsidyProgAddDTO = new SubsidyProgAddDTO();
        subsidyProgAddDTO.setName(getRandomValue("varchar",255));
        subsidyProgAddDTO.setRemark(getRandomValue("varchar",255));
        subsidyProgAddDTO.setTaxMethod(SafeKit.getInteger(getRandomValue("tinyint",4)));
        subsidyProgAddDTO.setAttendanceRange(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        subsidyProgAddDTO.setAttendanceRangeDays(SafeKit.getInteger(getRandomValue("int",11)));
        subsidyProgAddDTO.setCompanyId(getRandomValue("varchar",32));
        return subsidyProgAddDTO;
    }


    /**
     * 生成update测试数据
     * @return SubsidyProgUpdateDTO
     */
    public SubsidyProgUpdateDTO getSubsidyProgUpdateDTO(SubsidyProgVO subsidyProgVO){
        return subsidyProgService.selectDOById(subsidyProgVO.getId(), SITE_ID).to(SubsidyProgUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return SubsidyProgQuery
     */
    public SubsidyProgQuery getSubsidyProgQuery() {
        SubsidyProgQuery subsidyProgQuery = new SubsidyProgQuery();
        List<SubsidyProgDO> subsidyProgDOList = subsidyProgService.list(new SubsidyProgDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(subsidyProgDOList)) {
            SubsidyProgDO subsidyProgDO = subsidyProgDOList.get(0);
            subsidyProgQuery = subsidyProgDO.to(SubsidyProgQuery.class);
            if (subsidyProgDO.getGmtCreate() != null) {
                subsidyProgQuery.setGmtCreateStart(DateKit.getBeforeDay(subsidyProgDO.getGmtCreate(),-1));
                subsidyProgQuery.setGmtCreateEnd(DateKit.getBeforeDay(subsidyProgDO.getGmtCreate(),1));
            }
            if (subsidyProgDO.getGmtModified() != null) {
                subsidyProgQuery.setGmtModifiedStart(DateKit.getBeforeDay(subsidyProgDO.getGmtModified(),-1));
                subsidyProgQuery.setGmtModifiedEnd(DateKit.getBeforeDay(subsidyProgDO.getGmtModified(),1));
            }
        }
        return subsidyProgQuery;
    }

    /**
     * 保存示例
     * @return SubsidyProgVO
     */
    public SubsidyProgVO saveSubsidyProgExample(){
        SubsidyProgAddDTO addDTO = this.getSubsidyProgAddDTO();
        SubsidyProgVO subsidyProgVO = subsidyProgService.save(addDTO,buildTokenUser());
        return subsidyProgVO;
    }

    /**
     * 批量生成add测试数据
     * @return moduleContext
     */
    public void batchSaveSubsidyProgExample(ModuleContext moduleContext) {
        List<EmpInfoVO> empInfoVOList = moduleContext.get(CODE_EMP_LIST);
//        List<SubsidyProgDO> subsidyProgDOList = new ArrayList<>();
        for(int i = 0; i < 5; i++){
            SubsidyProgAddDTO subsidyProgAddDTO = new SubsidyProgAddDTO();
            subsidyProgAddDTO.setName(getRandomValue("varchar",255));
            subsidyProgAddDTO.setRemark(getRandomValue("varchar",255));
            subsidyProgAddDTO.setTaxMethod(SafeKit.getInteger(getRandomValue("tinyint",4)));
            subsidyProgAddDTO.setAttendanceRange(SafeKit.getBoolean(getRandomValue("tinyint",1)));
            subsidyProgAddDTO.setAttendanceRangeDays(SafeKit.getInteger(getRandomValue("int",11)));
            subsidyProgAddDTO.setCompanyId(empInfoVOList.get(0).getCompanyId());
            List<SubsidyInfoDO> subsidyInfoDOList = new ArrayList<>();

            for(int j = 0; j < 3; j++){
                SubsidyInfoAddDTO subsidyInfoAddDTO = new SubsidyInfoAddDTO();
                subsidyInfoAddDTO.setProgId("");
                subsidyInfoAddDTO.setName(getRandomValue("varchar",255));
                subsidyInfoAddDTO.setType(SafeKit.getInteger(getRandomValue("tinyint",4)));
                subsidyInfoAddDTO.setSendDate(SafeKit.getInteger(getRandomValue("int",11)));
                subsidyInfoAddDTO.setAmount(SafeKit.getInteger(getRandomValue("int",11)));
                subsidyInfoAddDTO.setCompanyId(empInfoVOList.get(0).getCompanyId());
                SubsidyInfoDO subsidyInfoDO = subsidyInfoAddDTO.to(SubsidyInfoDO.class);
                subsidyInfoDO.setSiteId(buildTokenUser().getSiteId());
                subsidyInfoDOList.add(subsidyInfoDO);
            }
            subsidyProgAddDTO.setSubsidyInfoDOList(subsidyInfoDOList);
//            subsidyProgDOList.add(subsidyProgAddDTO.to(SubsidyProgDO.class));

            ApplyToProgAddDTO applyToProgAddDTO = new ApplyToProgAddDTO();
            applyToProgAddDTO.setBusProgType(ProgTypeEnum.SUBSIDY_PROG.getValue());
            applyToProgAddDTO.setEmployType(SafeKit.getString(i + 1));
            applyToProgAddDTO.setEmployRange(SafeKit.getString(i + 1));
            applyToProgAddDTO.setMutex(false);
            applyToProgAddDTO.setCompanyId(empInfoVOList.get(0).getCompanyId());

            List<ApplyToProgDepAddDTO> applyToProgDepAddDTOList = new ArrayList<>();
            ApplyToProgDepAddDTO applyToProgDepAddDTO = new ApplyToProgDepAddDTO();
            applyToProgDepAddDTO.setType(2);
            applyToProgDepAddDTO.setDepId(SafeKit.getString(i + 1));
            applyToProgDepAddDTO.setProgId(empInfoVOList.get(0).getCompanyId());
            applyToProgDepAddDTO.setProgId("");
            applyToProgDepAddDTOList.add(applyToProgDepAddDTO);

            List<ApplyToProgPositionAddDTO> applyToProgPositionAddDTOList = new ArrayList<>();
            ApplyToProgPositionAddDTO applyToProgPositionAddDTO = new ApplyToProgPositionAddDTO();
            applyToProgPositionAddDTO.setPositionId(SafeKit.getString(i + 1));
            applyToProgPositionAddDTO.setCompanyId(empInfoVOList.get(0).getCompanyId());
            applyToProgPositionAddDTO.setProgId("");
            applyToProgPositionAddDTOList.add(applyToProgPositionAddDTO);

            applyToProgAddDTO.setApplyToProgDepAddDTOList(applyToProgDepAddDTOList);
            applyToProgAddDTO.setApplyToProgPositionAddDTOList(applyToProgPositionAddDTOList);

            subsidyProgAddDTO.setApplyToProgAddDTO(applyToProgAddDTO);

            subsidyProgService.addSubsidyProg(subsidyProgAddDTO, buildTokenUser());
        }

        //查询津贴方案列表
        SubsidyProgQuery subsidyProgQuery = new SubsidyProgQuery();
        subsidyProgQuery.setCompanyId(empInfoVOList.get(0).getCompanyId());
        List<SubsidyProgListVO> subsidyProgListVOList = subsidyProgService.select(subsidyProgQuery, buildTokenUser().getSiteId());
        moduleContext.add(SUBSIDY_PROG_VO_LIST, subsidyProgListVOList);
    }
}

